package com.guodu.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.guodu.entity.UserInfo;

public class AdminRoleFilter implements Filter{
	
	 protected UserInfo sb = null;
	 protected String url = null;
	 protected FilterConfig filterConfig = null;

	public void destroy() {
		 this.url = null;
	     this.filterConfig = null;
		
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {
		  HttpSession session = ((HttpServletRequest)req).getSession();
		  
		  String urls = ((HttpServletRequest)req).getRequestURI().toString();//
		  
		  this.sb = (UserInfo) session.getAttribute("userbean");
		if (this.sb == null) {
			if(!urls.equals(this.url) && !urls.equals("/zdt/")){
				((HttpServletResponse) res).sendRedirect(this.url+"?result=1");
				return;
			}
			
		} else{
		}
		  
		fc.doFilter(req, res);
		
	}

	public void init(FilterConfig fc) throws ServletException {
		 this.filterConfig = fc;
	     this.url = filterConfig.getInitParameter("url");
	     this.sb = null;
		
	}

}